package GUI;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import DB.DBImporter;

import com.cloudgarden.resource.SWTResourceManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Importer extends org.eclipse.swt.widgets.Composite {
	private Button importButton;
	private Label label1;
	private Label status;
	private Label label2;
	private Label fileNamelabel;
	private ProgressBar progressBar1;
	static Shell shell;
	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}

	public Importer(Composite parent, int style) {
		super(parent, style);
		initGUI();
	}
	
	/**
	* Initializes the GUI.
	*/
	private void initGUI() {
		try {
			this.setSize(245, 116);
			this.setBackground(SWTResourceManager.getColor(192, 192, 192));
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				FormData statusLData = new FormData();
				statusLData.left =  new FormAttachment(0, 1000, 82);
				statusLData.top =  new FormAttachment(0, 1000, 72);
				statusLData.width = 163;
				statusLData.height = 15;
				status = new Label(this, SWT.NONE);
				status.setLayoutData(statusLData);
			}
			{
				label2 = new Label(this, SWT.NONE);
				FormData label2LData = new FormData();
				label2LData.left =  new FormAttachment(0, 1000, 0);
				label2LData.top =  new FormAttachment(0, 1000, 72);
				label2LData.width = 82;
				label2LData.height = 15;
				label2.setLayoutData(label2LData);
				label2.setText("status:");
			}
			{
				FormData fileNamelabelLData = new FormData();
				fileNamelabelLData.left =  new FormAttachment(0, 1000, 64);
				fileNamelabelLData.top =  new FormAttachment(0, 1000, 51);
				fileNamelabelLData.width = 183;
				fileNamelabelLData.height = 15;
				fileNamelabel = new Label(this, SWT.NONE);
				fileNamelabel.setLayoutData(fileNamelabelLData);
			}
			{
				label1 = new Label(this, SWT.NONE);
				FormData label1LData = new FormData();
				label1LData.left =  new FormAttachment(0, 1000, 0);
				label1LData.top =  new FormAttachment(0, 1000, 51);
				label1LData.width = 65;
				label1LData.height = 15;
				label1.setLayoutData(label1LData);
				label1.setText("File:");
			}
			{
				FormData progressBar1LData = new FormData();
				progressBar1LData.left =  new FormAttachment(0, 1000, 0);
				progressBar1LData.top =  new FormAttachment(0, 1000, 93);
				progressBar1LData.width = 245;
				progressBar1LData.height = 17;
				progressBar1 = new ProgressBar(this, SWT.NONE);
				progressBar1.setLayoutData(progressBar1LData);
			}
			{
				importButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData importButtonLData = new FormData();
				importButtonLData.left =  new FormAttachment(0, 1000, 87);
				importButtonLData.top =  new FormAttachment(0, 1000, 12);
				importButtonLData.width = 73;
				importButtonLData.height = 33;
				importButton.setLayoutData(importButtonLData);
				importButton.setText("IMPORT");
				importButton.addMouseListener(new MouseAdapter() {
					public void mouseDown(MouseEvent evt) {
						importButtonMouseDown(evt);
					}
				});
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void importButtonMouseDown(MouseEvent evt) {
		importButton.setEnabled(false);
		final Display display = Display.getDefault();
		new Thread(){public void run(){
			if (display.isDisposed())
			    return;
			  while (DBImporter.bytesreceived!=DBImporter.totalbytes || DBImporter.bytesreceived==0){
	        	 try {Thread.sleep(50);} catch (Throwable th) {}
				 display.asyncExec(new Runnable() {
			        public void run() {
				          if (progressBar1.isDisposed())
				            return;
				          if (DBImporter.totalbytes!=0){
				        	  fileNamelabel.setText(DBImporter.currentFile);
				        	  status.setText(DBImporter.state+" "+100*DBImporter.bytesreceived/DBImporter.totalbytes+"%");
				        	  progressBar1.setSelection(100*DBImporter.bytesreceived/DBImporter.totalbytes);
				          }
				        }
				      });
			  }
			  display.asyncExec(new Runnable() {
			        public void run() {
			        	shell.close();
			        }});
		}}.start();
		new Thread(){public void run(){DBImporter.makeIMPORT();}}.start();
	}

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		Display display = Display.getDefault();
		shell = new Shell(display);
		Importer inst = new Importer(shell, SWT.None);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

}
